/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _MAIN_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "main.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
int main(int argc, char *argv[])
{
	int 	i = 0;
	int 	sockfd;
	int 	len,lenge;
	struct sockaddr_in address;
	int 	result,Nut,answer;

	if(argc == 3)
	{
		if((argv[1][0]=='1')||(argv[1][0]=='2')||(argv[1][0]=='3'))
		{
			while(argv[2][i] != '\0')
			{
				if(!((argv[2][i] == 'A')||(argv[2][i] == 'B')||(argv[2][i] == 'C')))
				{
				printf("Error,the third argument must be string 'A','B' or 'C' only\n");
				return(0);
				}
				i++;
			}

			// Create a socket for the client. 
			sockfd = socket(AF_INET, SOCK_STREAM, 0);
			// Socket name, as agreed with the server.
			address.sin_family = AF_INET;
			address.sin_addr.s_addr = inet_addr("192.168.1.245");
			address.sin_port = 1803;
			len = sizeof(address);
	
			// Connect our socket to the server's socket.
			result = connect(sockfd, (struct sockaddr *)&address, len);
			if(result == -1)
			{
				perror("OOPS!!: connect function error");
				exit(1);
			}
	
			// Write memory to server 
			lenge = strlenge(argv[2]);
			write(sockfd, &lenge,sizeof(int));
			result = read(sockfd, &answer, sizeof(int));
			if(answer)
			{
				// Write start position
				write(sockfd, &argv[1][0],sizeof(char));
				result = read(sockfd, &answer, sizeof(int));
				if(answer)
				{
					// Write string
					write(sockfd, argv[2],lenge*sizeof(char));
					result = read(sockfd, &answer, sizeof(int));
					if(answer)
					{
						read(sockfd, &Nut, sizeof(int));
						printf("%d\n",Nut);
					}
					else
					{
						printf("Server not reply about string\n");
					}
					
				}
				else
				{
					printf("Server not reply about position\n");
				}
			}
			else
			{
				printf("Server not reply about memory\n");
			}

			/* Close socket and exit program. */
			close(sockfd);
			exit(0);
		}
		else
		{
			printf("Error,the number of second argument must be 1,2 or 3\n");
		}		
	}
	else
	{
		printf("Error,the number of argument must be 3\n");
	}

}

/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/
 
